Online-Academy
Look, Read, Understand, Apply

UI - Python

Creating User Interface using tkinter
  • First import tkinter:
    import tkinter as tk
  • create container (window) using Tk() method, it is called by tkinter
    root = tk.Tk()
  • We can specify size of container using geometry method:
    root.geometry("300*300"). We have to pass dimensions as string.
  • Controls like label, textbox, button can be created as following:

    label = tk.Label(root, text="Enter Name: ")

    textbox = tk.Text(root, height = 2, width=50)

    We can use tk.Entery() method to create textbox with height=1,
    textbox2 = tk.Entery(root, width=50)

    button = tk.Button(root,text="Click here")

In all of the control creation methods, first parameter passed is reference to container object, here root

We can assign event to control by pass command parameter to the control creation method. that is:
button = tk.Button(root,text="submit",command = click)
here, click is an function

import tkinter as tk

def click():
    print("click")

root = tk.Tk()
root.geometry("300x300")
label = tk.Label(root, text="Hello World",font=("Arial", 25))
label.pack(padx=10, pady=10)
textbox = tk.Text(root, height=2, width=50)
textbox.pack(padx=10, pady=10)
button = tk.Button(root, text="Click me",command=click)
button.pack(padx=10, pady=10)
chk = tk.Checkbutton(root, text="Check")
chk.pack(padx=10, pady=10)
root.mainloop()